package imper.engine.battle.engine.map;

import imper.engine.battle.entity.IBatMap;
import imper.engine.battle.entity.IBatMapCase;
import imper.engine.battle.entity.IBatPosition;
import imper.engine.entity.Position;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class VisualReaderMap extends ReaderMapEngine
{
	protected Graphics _graphics;

	protected int _sizeX;
	protected int _sizeY;

	protected Position _position;

	protected Image image = null;
	protected Image image2 = null;

	public VisualReaderMap(IBatMap pMap, int pSizeX, int pSizeY)
	{
		super( pMap );
		_sizeX = pSizeX;
		_sizeY = pSizeY;
	}

	public void setGraphics( Graphics pGraphics , Position pPosition )
	{
		_graphics = pGraphics;
		_position = pPosition;
	}

	public void init()
	{
		try
		{
			image = new Image( "data/hexa/hexagone1.png" );
			//image2 = new Image( "data/hexa/unit.png" );
		}
		catch ( SlickException e )
		{
			e.printStackTrace();
		}
	}

	@Override
	protected void readCase( IBatMapCase pCase )
	{
		if ( pCase.getPosition().getPosX() < _position.getPosX() )
		{
			return;
		}
		if ( pCase.getPosition().getPosY() < _position.getPosY() )
		{
			return;
		}
		if ( pCase.getPosition().getPosX() > _position.getPosX() + _sizeX )
		{
			return;
		}
		if ( pCase.getPosition().getPosY() > _position.getPosY() + _sizeY )
		{
			return;
		}
		if ( pCase.getPosition().getPosX() <= 1 && pCase.getPosition().getPosY() <= 1 )
		{
			// System.out.println("*"+pCase.getPosition().getPosX()+
			// ":"+pCase.getPosition().getPosY());
		}
		/*
		 * int posX = pCase.getPosition().getPosX() * 72; // double height =
		 * Math.sqrt(3.0)/2.0 * 48.0; int posY = pCase.getPosition().getPosY() *
		 * 21; if ( pCase.getPosition().getPosY() % 2 == 0 ) {
		 * 
		 * // posY = posY - 48 / 2; } else { posX = posX + 36; // posX =
		 * pCase.getPosition().getPosX() * 36;
		 * 
		 * }
		 * 
		 * try { Image image = new Image( "data/hexa/hexagone1.png" );
		 * _graphics.drawImage( image , posX , posY ); } catch ( SlickException
		 * e ) { e.printStackTrace(); }
		 */
		Position pos = transformPositionInScreen( pCase.getPosition() );

		_graphics.drawImage( image , pos.getPosX() , pos.getPosY() );
		if ( pCase.getUnit() != null )
		{
			//_graphics.drawImage( image2 , pos.getPosX() , pos.getPosY() );
		}

	}

	public static Position transformPositionInScreen( IBatPosition pPos )
	{
		int posX = pPos.getPosX() * 42;
		int posY = pPos.getPosY() * 36;
		if ( pPos.getPosY() % 2 != 0 )
		{
			posX = posX + 21;
		}
		return new Position( posX, posY );
	}
}
